<template>
	<view class="personal-edit">
		<view class="container">
			<uni-list>
				<uni-list-item link>
					<template v-slot:body>
						<button class="avatar-row" open-type="chooseAvatar" @chooseavatar="chooseAvatar">
							<view class="label">头像</view>
							<view class="avatar-box">
								<image mode="aspectFill" :src="avatarUrl ? avatarUrl : getDefaultImage()"
									@error="avatarUrl = getDefaultImage()" />
							</view>
						</button>
					</template>
				</uni-list-item>
				<uni-list-item>
					<template v-slot:header>
						<view class="label">昵称</view>
					</template>
					<template v-slot:footer>
						<input type="nickname" class="sign-input" placeholder="请输入昵称" placeholder-style="color: #bdbdbd;"
							v-model="username" />
					</template>
				</uni-list-item>
				<uni-list-item link @click="getMobile">
					<template v-slot:header>
						<view class="label">更换手机号</view>
					</template>
					<template v-slot:footer>
						<text class="mobile" v-if="mobile">{{mobile}}</text>
						<text class="mobile-placeholder" v-else>请绑定手机号</text>
					</template>
				</uni-list-item>
				<uni-list-item link to="/pages/passwordEdit/passwordEdit">
					<template v-slot:header>
						<view class="label">修改密码</view>
					</template>
				</uni-list-item>
			</uni-list>
		</view>
		<view class="btn" @click="save">保存</view>
		<safe-area></safe-area>
		<uni-popup type="bottom" height="40%" background-color="transparent" :safe-area="false" ref="mobileRef"
			@close="mobilePopupClose">
			<view class="mobilePopup">
				<view class="item">
					<input :maxlength="11" v-model="mobilePopup.mobile" placeholder-style="color: #bdbdbd;"
						placeholder="请输入手机号" />
				</view>
				<view class="popup-btns">
					<view class="popup-btn" @click="mobilePopupCancel">取消</view>
					<view class="popup-btn" @click="mobilePopupConfirm">确定</view>
				</view>
				<safe-area></safe-area>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		pathToBase64,
	} from "image-tools";
	import {
		userInfo,
	} from "@/api/search_read.js";
	import {
		personEdit,
	} from "@/api/call_kw.js";
	import _ from "lodash";
	export default {
		data() {
			return {
				id: '',
				// 头像
				avatarUrl: '',
				// 用户名
				username: '',
				// 手机号
				mobile: '',
				// 手机号输入弹窗
				mobilePopup: {
					mobile: '',
				},
			}
		},
		onLoad() {
			this.getInfo();
		},
		methods: {
			// 获取用户信息
			getInfo() {
				userInfo().then(res => {
					if (res.data?.result && res.data?.result?.length >= 1) {
						const info = res.data?.result?.records[0];
						this.id = info.id || '';
						this.avatarUrl = info.avatar_256 ? this.image_base64(info.avatar_256) : '';
						this.username = info.name || '';
						this.mobile = info.mobile_phone || '';
					} else {
						uni.showToast({
							icon: 'none',
							title: '个人资料获取失败!',
						})
						uni.navigateBack();
					}
				}).catch(() => {
					uni.showToast({
						icon: 'none',
						title: '系统错误',
					})
				})
			},
			image_base64(photo) {
				return 'data:image/jpeg;base64,' + photo;
			},
			// 默认头像
			getDefaultImage() {
				return require("@/static/image/avatar-default.png");
			},
			// 获取头像(微信默认头像)
			chooseAvatar(e) {
				pathToBase64(e.detail.avatarUrl).then(base64 => {
					this.avatarUrl = base64 || '';
				})
			},
			// 获取手机号
			getMobile: _.debounce(function() {
				this.$refs.mobileRef.open();
			}, 300),
			// 手机号弹窗点击取消
			mobilePopupCancel() {
				this.$refs.mobileRef.close();
			},
			// 手机号弹窗关闭回调
			mobilePopupClose() {
				this.mobilePopup.mobile = '';
			},
			// 手机号弹窗点击确定
			mobilePopupConfirm: _.debounce(function() {
				if (this.mobilePopup.mobile.length !== 11) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的手机号'
					})
					return;
				}
				this.mobile = this.mobilePopup.mobile;
				this.mobilePopupCancel();
			}, 300),
			// 点击保存
			save: _.debounce(function() {
				uni.showLoading({
					title: '提交中...',
				})
				personEdit({
					id: this.id,
					name: this.username,
					phone: this.mobile,
					image_1920: this.avatarUrl.split(',')[1]
				}).then(res => {
					uni.hideLoading();
					uni.showToast({
						icon: 'success',
						title: '保存成功!'
					})
					setTimeout(() => {
						uni.navigateBack();
					}, 1500)
				}).catch(() => {
					uni.hideLoading();
					uni.showToast({
						icon: 'none',
						title: '系统错误',
					})
				})
			}, 300),
		},
	}
</script>

<style lang="scss" scoped>
	.personal-edit {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;

		.container {
			width: 100%;

			.avatar-row {
				padding: 0;
				background-color: transparent !important;
				width: 750rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				&::after {
					border: none;
				}
			}

			.label {
				margin: auto 0;
				color: black;
				font-size: 32rpx;
			}

			.avatar-box {
				width: 90rpx;
				height: 90rpx;
				border-radius: 90rpx;
				overflow: hidden;
				flex-shrink: 0;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.sign-input {
				text-align: right;
			}

			.mobile {
				font-size: 30rpx;
			}

			.mobile-placeholder {
				font-size: 30rpx;
				color: #bdbdbd;
			}
		}

		.btn {
			margin: auto 0 250rpx;
			width: 380rpx;
			height: 80rpx;
			border-radius: 10rpx;
			background-color: #3F7CFB;
			line-height: 80rpx;
			text-align: center;
			color: white;
		}
	}

	.mobilePopup {
		width: 100%;
		background-color: white;
		display: flex;
		flex-direction: column;
		padding: 60rpx 0 30rpx;
		border-radius: 20rpx 20rpx 0 0;

		.item {
			width: 100%;
			padding: 0 50rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;

			input {
				flex: 1;
				margin-right: 30rpx;
			}

			.wxPhoneNumberScope {
				margin: 0 !important;
				padding: 0 40rpx;
				background-color: rgb(7, 193, 96);
				color: white;
				font-size: $uni-font-size-base;

				&::after {
					border: none;
				}
			}
		}

		.popup-btns {
			margin-top: 80rpx;
			width: 100%;
			padding: 0 50rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;

			.popup-btn {
				flex: 1;

				&:not(:first-child) {
					margin-left: 50rpx;
				}

				height: 60rpx;
				border-radius: 10rpx;
				line-height: 60rpx;
				text-align: center;
				background-color: white;
				border: 1px solid $uni-primary;
				color: $uni-primary;

				&:nth-child(2) {
					background-color: $uni-primary;
					color: white;
				}
			}
		}
	}
</style>